﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class search : AbstractPage {
	protected void Page_Load(object sender, EventArgs e) {
		User user = (User) Session[AbstractPage.SESSION_USER];
		if (!this.IsPostBack) {
			this.txtbxZip.Text = user.Destinations[0].Zip.ToString();
		}
	}

	protected void btnSearch_Click(object sender, EventArgs e) {
		User user = (User) Session[AbstractPage.SESSION_USER];
		IScheduleService scheduleService = new ScheduleServiceImpl();
		ScheduleSearchCriteria searchCriteria = new ScheduleSearchCriteria();

		searchCriteria.DayOfWeek = this.SelectDay();

		try {
			searchCriteria.Zip = int.Parse(this.txtbxZip.Text);
		} catch {
			this.addMessage(new Message(Message.Type.ERROR, "Zip code must be a number and is a required field"));
		}

		if (this.txtbxReturn.Text.Length > 0) {
			try {
				searchCriteria.ReturnHour = int.Parse(txtbxReturn.Text);
			} catch {
				this.addMessage(new Message(Message.Type.ERROR, "Arrival time must be a number 0 - 23"));
			}
		}

		if (this.txtbxDepart.Text.Length > 0) {
			try {
				searchCriteria.DepartureHour = int.Parse(txtbxDepart.Text);
			} catch {
				this.addMessage(new Message(Message.Type.ERROR, "Departure time must be a number 0 - 23"));
			}
		}

		searchCriteria.thisUser = user;

		if (this.messages.Count == 0) {
			this.Context.Items[AbstractPage.SEARCH_RESULTS] = scheduleService.findSchedules(searchCriteria);
			this.Server.Transfer("searchResults.aspx", true);
		}
	}

	protected void btnCancel_Click(object sender, EventArgs e) {
		Response.Redirect("mainMenu.aspx");
	}

	protected DayOfWeek? SelectDay() {
		DayOfWeek? result = null;
		if (radbtnMon.Checked) {
			result = DayOfWeek.Monday;
		} else if (radbtnTues.Checked) {
			result = DayOfWeek.Tuesday;
		} else if (radbtnWed.Checked) {
			result = DayOfWeek.Wednesday;
		} else if (radbtnThurs.Checked) {
			result = DayOfWeek.Thursday;
		} else if (radbtnFri.Checked) {
			result = DayOfWeek.Friday;
		} else {
			this.messages.Add(new Message(Message.Type.ERROR, "Day of week not selected"));
		}

		return result;
	}
}
